package com.liberation.aidiancan.base.rebaseadapter;


import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */
//如果要加载动画，添加头布局就可
//注：如果使用GridLayoutManager需要在设置的时候添加以下代码


/*gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
        return (adapter.isHeaderView(position) || adapter.isBottomView(position)) ? gridLayoutManager.getSpanCount() : 1;
        }
        });*/

public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<MyHolder> {

    private List<T> data;
    private int contentResId;
    public int oneitemId;
    //布局id
    private int headResId = 0;
    private int bootResId = 0;
    public View headView;
    //布局类型
    public static final int ITEM_TYPE_HEADER = 5;
    public static final int ITEM_TYPE_CONTENT = 1;
    public static final int ITEM_TYPE_BOTTOM = 2;
    public static final int ITEM_TYPE_CONTENT_ONE = 3;


    private int mHeaderCount = 0;//头部View个数
    private int mBottomCount = 0;//底部View个数


    //添加头布局
    public void addHeadView(int resId) {
        headResId = resId;
        mHeaderCount += 1;

    }

    //单部据设置id
    public void setOneitemResId(int resId) {
        oneitemId = resId;
    }


    public void addFootView(int resId) {
        bootResId = resId;
        mBottomCount += 1;
    }

    //判断是否为单布局
    public boolean isOneitem(int position) {
        if (position >= mHeaderCount) {
            return itemType(position - mHeaderCount) == ITEM_TYPE_CONTENT_ONE;
        }
        return false;
    }


    //内容长度
    public int getContentItemCount() {
        //解决可能的空指针
        return data==null?0:data.size();
    }

    //判断当前item是否是HeadView
    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    //判断当前item是否是FooterView
    public boolean isBottomView(int position) {
        return mBottomCount != 0 && position >= (mHeaderCount + getContentItemCount());
    }

    //判断当前item类型
    @Override
    public int getItemViewType(int position) {
    //    Log.e("msg", "getItemViewType: "+position);
        if (isHeaderView(position)) {
//头部View
            return ITEM_TYPE_HEADER;
        } else if (isBottomView(position)) {
//底部View
            return ITEM_TYPE_BOTTOM;
        } else if (isOneitem(position)) {
            return ITEM_TYPE_CONTENT_ONE;
        } else {
//内容View

            return ITEM_TYPE_CONTENT;
        }
    }

    public abstract int itemType(int position);
//================================================================

    //构造方法
    public RecyclerAdapter(int rsId, List<T> data) {
        this.data = data;
        this.contentResId = rsId;
    }

    //根据不同类型创建不同布局的ViewHolder
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder myHolder = null;
      //  Log.e("msg", "getItemViewType333333333333: ");
        switch (viewType) {
            case ITEM_TYPE_HEADER: {
                headView = LayoutInflater.from(parent.getContext()).inflate(headResId, null);
                myHolder = new MyHolder(headView);
                myHolder.HOlder_TYPE = ITEM_TYPE_HEADER;
            }
            break;
            case ITEM_TYPE_CONTENT: {
                myHolder = new MyHolder(LayoutInflater.from(parent.getContext()).inflate(contentResId, null));
                myHolder.HOlder_TYPE = ITEM_TYPE_CONTENT;

            }
            break;
            case ITEM_TYPE_CONTENT_ONE: {
                if (oneitemId == 0) {
                    Logger.e("没有传递单布局id");
                    break;
                }
                myHolder = new MyHolder(LayoutInflater.from(parent.getContext()).inflate(oneitemId, null));
                //type = ITEM_TYPE_CONTENT;
                myHolder.HOlder_TYPE = ITEM_TYPE_CONTENT_ONE;

            }
            break;

            case ITEM_TYPE_BOTTOM: {
                myHolder = new MyHolder(LayoutInflater.from(parent.getContext()).inflate(bootResId, null));
                myHolder.HOlder_TYPE = ITEM_TYPE_BOTTOM;
            }
            break;

        }

        return myHolder;

    }

    //绑定数据
    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
     //   Log.e("msg", "getItemViewType333333333333: "+position);
        switch (holder.HOlder_TYPE) {
            case ITEM_TYPE_HEADER: {
                onBindHeadViewHolder(holder, position);
            }
            break;
            case ITEM_TYPE_BOTTOM: {
                onBindFootViewHolder(holder, position);
            }
            break;
            case ITEM_TYPE_CONTENT: {
                if (position >= mHeaderCount && data.size() != 0) {
                    onBindMyViewHolder(holder, position - mHeaderCount, data);
                }
            }
            break;
            case ITEM_TYPE_CONTENT_ONE: {
                Log.e("msg", "onBindViewHolder: " + position);
                if (position >= mHeaderCount && data.size() != 0) {
                    onBindMyViewHolder(holder, position - mHeaderCount, data);
                }


            }
            break;
        }

        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onItemLick(holder, position - mHeaderCount);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    clickListener.onLongItemLick(holder, position - mHeaderCount);
                    return true;
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return mHeaderCount + getContentItemCount() + mBottomCount;
    }
    //=================================================================

    //自定义数据绑定方法，将对应holder,position,data传到继承的item中
    public abstract void onBindMyViewHolder(MyHolder holder, int position, List<T> data);
    //设置头布局
    public void onBindHeadViewHolder(MyHolder holder, int position) {
    }

    public void onBindFootViewHolder(MyHolder holder, int position) {
    }


    //事件回调*******************************************************
    OnItemLickListener clickListener;

    public void setOnitemClickListener(OnItemLickListener itemLickListener) {
        clickListener = itemLickListener;
    }

    public interface OnItemLickListener {
        void onItemLick(MyHolder holder, int position);

        void onLongItemLick(MyHolder holder, int position);
    }




}
